<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/table.php";
require_once $basedir."/inc/adress.php";
require_once $basedir."/inc/gruppenkinder.php";
require_once $basedir."/inc/vertraege.php";

class Kinder extends table {
    
    public static $TABLENAME = "kinder";
    public static $CLASSNAME = "Kinder";
    
    private $id = "";
    private $vorname = "";
    private $nachname = "";
    private $name_show = 0;
	private $geburtsdatum;
    private $adresse_id;
    private $adresse_show = 0;
    private $mutter_id;
    private $mutter_sorge = 0;
    private $vater_id;
    private $vater_sorge = 0;
	private $migration;
	private $zurueckgestellt;
    private $bilder_druck = 0;
    private $bilder_online = 0;
    
    public function __construct($id="") {
		if($id!='') {
            $temp = Database::getDefault()->openId(Kinder::$TABLENAME,$id);
            foreach($temp as $key => $value) {
                $this->$key = $value;
            }
		}
    }
    
	public function getJsonData() {
		$var = get_object_vars($this);
		foreach($var as &$value) {
			if(is_object($value) && method_exists($value, 'getJsonData')) {
				$value = $value->getJsonData();
			} else {
			    $value = utf8_encode($value);
			}
		}
		return $var;
	}
	
	static public function search($value) {
	    global $VEREIN;
		$query = "SELECT DISTINCT * FROM " . $VEREIN['prefix'] . Kinder::$TABLENAME . " WHERE nachname LIKE '%".$value."%' OR vorname LIKE '%".$value."%' ORDER BY nachname, vorname";
		return Database::getDefault()->execute($query,Kinder::$CLASSNAME);
	}
	
    public function delete() {
        Database::getDefault()->delete(Kinder::$TABLENAME,$this->id);
    }
    
    public function save() {
    	$values = array();
		foreach ($this as $key => $value) {
			$values[$key] = $value;
		}
        if($this->id=="") {
            $this->id = Database::getDefault()->insert(Kinder::$TABLENAME, $values);
        } else {
            Database::getDefault()->update(Kinder::$TABLENAME, $values, $this->id);
        }
    }
    
    public function getId() {
        return $this->id;
    }
    
    public function getCurrentContract($date="") {
        global $VEREIN;
        if($date=="") {
            $date = date("Y-m-d");
        }
        $query = "SELECT * FROM " . $VEREIN['prefix'] . "vertraege WHERE kind_id='".$this->id."' AND (ausgeschieden_am>'".$date."' OR ausgeschieden_am='0000-00-00') ORDER BY vertrag_ab DESC";
        $values = Database::getDefault()->execute($query, "Vertraege");
        if(count($values) > 0) {
            return $values[0];
        }
        return null;
    }
    
	public function getDarlehen() {
	    global $VEREIN;
		$query = "SELECT darlehen FROM " . $VEREIN['prefix'] . "vertraege WHERE kind_id='".$this->id."' AND darlehen<>0";
		$values = Database::getDefault()->execute($query);
		return $values[0]['darlehen'];
	}
	
    public function setVorname($value) {
        $this->vorname = $value;
    }
    
    public function getVorname() {
        return $this->vorname;
    }
    
    public function setNachname($value) {
        $this->nachname = $value;
    }
    
    public function getNachname() {
        return $this->nachname;
    }
    
    public function setNameShow($value) {
        $this->name_show = $value;
    }
    
    public function isNameShow() {
        return $this->name_show;
    }
    
	public function setGeburtsdatum($value) {
		$this->geburtsdatum = $value;
	}
	
	public function getGeburtsdatum() {
		return $this->geburtsdatum;
	}
	
    public function isSchulkind() {
        global $VEREIN;
        $year = date("y");
        if(date("m") > 7) {
            $year++;
        }
        $temp = explode("-", $year.$VEREIN['stichtag_schule']);
        $stichtag = mktime(0,0,0,$temp[1],$temp[2],$temp[0]);
        $geburtstag = explode("-", $this->getGeburtsdatum());
        $geburtstag = mktime(0,0,0,$geburtstag[1],$geburtstag[2],$geburtstag[0]);
        $age = intval($stichtag - $geburtstag) / (3600 * 24 * 365);
        return $age>=6?true:false;
    }
    
    public function setAdresseId($value) {
        $this->adresse_id = $value;
    }
    
    public function getAdresseId() {
        return $this->adresse_id;
    }
    
	public function getAdresse() {
		return new Adresse($this->getAdresseId());
	}
    
    public function setAdresseShow($value) {
        $this->adresse_show = $value;
    }
    
    public function isAdresseShow() {
        return $this->adresse_show;
    }
	
    public function setMutterId($value) {
        $this->mutter_id = $value;
    }
    
    public function getMutterId() {
        return $this->mutter_id;
    }
	
	public function getMutter() {
	    if($this->getMutterId()=="" || $this->getMutterId()==0) {
	        return new Adresse();
	    }
		return new Adresse($this->getMutterId());
	}
    
    public function setMutterSorge($value) {
        $this->mutter_sorge = $value;
    }
    
    public function isMutterSorge() {
        return $this->mutter_sorge;
    }
    
    public function setVaterId($value) {
        $this->vater_id = $value;
    }
    
    public function getVaterId() {
        return $this->vater_id;
    }
    
	public function getVater() {
	    if($this->getVaterId()=="" || $this->getVaterId()==0) {
	        return new Adresse();
	    }
		return new Adresse($this->getVaterId());
	}
	
    public function setVaterSorge($value) {
        $this->vater_sorge = $value;
    }
    
    public function isVaterSorge() {
        return $this->vater_sorge;
    }
    
	public function setMigration($value) {
		$this->migration = $value;
	}
	
	public function isMigration() {
	    if(!$this->getMutter()->isDeutsch())
            return 1;
        if(!$this->getVater()->isDeutsch())
            return 1;
		return $this->migration;
	}
	
	public function setZurueckgestellt($value) {
		$this->zurueckgestellt = $value;
	}
	
	public function getZurueckgestellt() {
		return $this->zurueckgestellt;
	}
	
	public function isZurueckgestellt() {
		return $this->zurueckgestellt!="";
	}
	
    public function setBilderDruck($value) {
        $this->bilder_druck = $value;
    }
    
    public function isBilderDruck() {
        return $this->bilder_druck;
    }
    
    public function setBilderOnline($value) {
        $this->bilder_online = $value;
    }
    
    public function isBilderOnline() {
        return $this->bilder_online;
    }
    
    public function getGruppenzuordnung() {
        global $VEREIN;
        $query = "SELECT DISTINCT * FROM " . $VEREIN['prefix'] . "gruppenkinder WHERE kind_id='".$this->id."'";
        return Database::getDefault()->execute($query, "GruppenKinder");
    }
    
    static public function getAll() {
        global $VEREIN;
        $query = "SELECT * FROM " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ORDER BY nachname, vorname";
        return Database::getDefault()->execute($query,Kinder::$CLASSNAME);
    }
    
    /**
     * create table for banking contacts or update if something to do
     */
    public static function initDatabase() {
        global $VEREIN;
        echo "<b>update / create " . Kinder::$TABLENAME . "</b><br />";
        $query = "CREATE TABLE IF NOT EXISTS " . $VEREIN['prefix'] . Kinder::$TABLENAME . " (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            die(Database::getDefault()->getConnection()->error);
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN vorname VARCHAR(50) DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN nachname VARCHAR(100) DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN name_show TINYINT(1) DEFAULT '0'";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN geburtsdatum DATE DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN adresse_id INT UNSIGNED DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN adresse_show TINYINT(1) DEFAULT '0'";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN mutter_id INT UNSIGNED DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN mutter_sorge TINYINT(1) DEFAULT '0'";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN vater_id INT UNSIGNED DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN vater_sorge TINYINT(1) DEFAULT '0'";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN migration TINYINT(1) DEFAULT '0'";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN zurueckgestellt VARCHAR(100) DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN bilder_druck TINYINT(1) DEFAULT '0'";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Kinder::$TABLENAME . " ADD COLUMN bilder_online TINYINT(1) DEFAULT '0'";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        echo "<br />";
    }
}
?>